package com.alibaba.citrus.ots.forecast.repository.impl;

import com.alibaba.citrus.ots.common.annotation.FacadeInvoker;
import com.alibaba.citrus.ots.common.annotation.RepositoryInvoker;
import com.alibaba.citrus.ots.forecast.repository.DealLineQueryRepository;
import com.alibaba.cz.base.tool.log.Log;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.otsforecastsales.domian.deallinequeue.dto.LoadDealLineQueueListRequest;
import com.epoch.app.otsforecastsales.domian.deallinequeue.model.DealLineQueue;
import com.epoch.app.otsforecastsales.domian.deallinequeue.service.DealLineQueueService;
import com.epoch.app.otsforecastsales.model.dto.DealLineQueueLoadListRequest;
import com.epoch.app.otsforecastsales.model.dto.DealLineSDO;
import com.epoch.app.otsforecastsales.model.enums.ForecastDealStatusSEO;
import com.epoch.app.otsforecastsales.model.enums.ForecastDealTypeSEO;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @Author yuanDeng
 * @Date 2021/10/15 15:28
 **/
@Service
public class DealLineQueryRepositoryImpl implements DealLineQueryRepository {

    private static Log log = Log.getLogger(DealLineQueryRepositoryImpl.class);

    @Resource
    DealLineQueueService dealLineQueueService;
    @Override
    @RepositoryInvoker
    public Result<List<DealLineQueue>> queryProgressQueueResult(DealLineSDO dealLineSDO) {

        LoadDealLineQueueListRequest dealLineQueueLoadListRequest=new LoadDealLineQueueListRequest();
        dealLineQueueLoadListRequest.setCreator(dealLineSDO.getCreator());
        List<Integer> dealStatus=new ArrayList<>();
        List<Integer> dealType=new ArrayList<>();
        if (Objects.nonNull(dealLineSDO.getDealType())){
            dealType.add(Integer.parseInt(dealLineSDO.getDealType()));
        }

        if (CollectionUtils.isNotEmpty(dealLineSDO.getForecastOrderLineIdList())){
            dealLineQueueLoadListRequest.setForecastOrderLineIdList(dealLineSDO.getForecastOrderLineIdList());
        }
        dealLineQueueLoadListRequest.setForecastOrderLineId(dealLineSDO.getForecastOrderLineId());
        dealLineQueueLoadListRequest.setDealStatuses(dealStatus);
        dealLineQueueLoadListRequest.setDealTypes(dealType);
        dealLineQueueLoadListRequest.setBatchNumber(dealLineSDO.getBatchNumber());

        Result<List<DealLineQueue>> listResult = dealLineQueueService.loadDealLineQueueList(dealLineQueueLoadListRequest);
        return listResult;
    }

    /**
     * queryFABBalloonVisible
     *
     * @param
     */
    @Override
    @FacadeInvoker
    public Result<Boolean> queryFABBalloonVisible(DealLineSDO dealLineSDO) {
        LoadDealLineQueueListRequest dealLineQueueLoadListRequest=new LoadDealLineQueueListRequest();
        dealLineQueueLoadListRequest.setCreator(dealLineSDO.getCreator());
        List<Integer> dealStatus=new ArrayList<>();
        //查询处理状态有处理中的数据
        dealStatus.add(ForecastDealStatusSEO.ANALYSIS_ING.getStatus());
        dealStatus.add(ForecastDealStatusSEO.ANALYSIS_ED.getStatus());
        dealStatus.add(ForecastDealStatusSEO.CONFIRM_ING.getStatus());
        dealStatus.add(ForecastDealStatusSEO.CONFIRM_ED.getStatus());
        dealStatus.add(ForecastDealStatusSEO.CHECk_ING.getStatus());
        dealStatus.add(ForecastDealStatusSEO.CHECk_ED.getStatus());
        List<Integer> dealType=new ArrayList<>();
        //分析处理活动检查的类型
        dealType.add(ForecastDealTypeSEO.BATCH_ANALYSIS.getDealTypeCode());
        dealType.add(ForecastDealTypeSEO.BATCH_CONFIRM.getDealTypeCode());
        dealType.add(ForecastDealTypeSEO.CHECK_ACTIVITIES.getDealTypeCode());
        dealLineQueueLoadListRequest.setDealStatuses(dealStatus);
        dealLineQueueLoadListRequest.setDealTypes(dealType);
        Result<List<DealLineQueue>> listResult = dealLineQueueService.loadDealLineQueueList(dealLineQueueLoadListRequest);
        log.info("打印下处理队列列表：{}", JSONObject.toJSONString(listResult));
        if (Objects.equals(listResult.getTotal(),0)){
            return Result.success(false);
        }
        return Result.success(true);
    }

    @Override
    @FacadeInvoker
    public Result<List<DealLineQueue>> BatchQuery(DealLineSDO dealLineSDO) {
        DealLineQueueLoadListRequest dealLineQueueLoadListRequest =new DealLineQueueLoadListRequest();
        Date now = new Date();
        Date afterDate = new Date(now .getTime() - 300000);
        dealLineQueueLoadListRequest.setLimit(100);
        String strDateFormat = "yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
        dealLineQueueLoadListRequest.setContent(sdf.format(afterDate));
        Result<List<DealLineQueue>> listResult = dealLineQueueService.queryStaleDataList(dealLineQueueLoadListRequest);
        return listResult;
    }
}
